//
//  KooCalendarHeader.m
//  KooIEDUCalendarDemo
//
//  Created by nowcoder on 2024.
//

#import "KooCalendarHeader.h"
#import "KooCalendarConfig.h"
#import <Masonry/Masonry.h>

@interface KooCalendarHeader ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *leftArrowButton;
@property (nonatomic, strong) UIButton *rightArrowButton;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@end

@implementation KooCalendarHeader

#pragma mark - Initialization

- (instancetype)init {
    return [self initWithConfig:[KooCalendarConfig defaultConfig]];
}

- (instancetype)initWithConfig:(KooCalendarConfig *)config {
    self = [super init];
    if (self) {
        self.config = config ?: [KooCalendarConfig defaultConfig];
        self.currentDate = [NSDate date];
        self.showNavigationArrows = NO;
        [self setupDateFormatter];
        [self setupUI];
        [self setupConstraints];
        [self updateAppearance];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.config = [KooCalendarConfig defaultConfig];
        self.currentDate = [NSDate date];
        self.showNavigationArrows = NO;
        [self setupDateFormatter];
        [self setupUI];
        [self setupConstraints];
        [self updateAppearance];
    }
    return self;
}

#pragma mark - Private Setup

- (void)setupDateFormatter {
    self.dateFormatter = [[NSDateFormatter alloc] init];
    self.dateFormatter.calendar = [NSCalendar currentCalendar];
    self.dateFormatter.dateFormat = self.config.monthFormat;
}

#pragma mark - UI Setup

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];
    
    // 使用懒加载的方式添加UI组件，避免重复创建
    [self addSubview:self.titleLabel];
    [self addSubview:self.leftArrowButton];
    [self addSubview:self.rightArrowButton];
}

- (void)setupConstraints {
    // 标题标签约束 - 左对齐布局
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.centerY.equalTo(self);
        make.right.lessThanOrEqualTo(self.rightArrowButton.mas_left).offset(-10);
    }];
    
    // 左箭头按钮约束
    [self.leftArrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.centerY.equalTo(self);
        make.width.height.equalTo(@30);
    }];
    
    // 右箭头按钮约束
    [self.rightArrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-15);
        make.centerY.equalTo(self);
        make.width.height.equalTo(@30);
    }];
}

- (void)updateAppearance {
    if (!self.config) return;
    
    // 更新标题样式
    self.titleLabel.textColor = self.config.monthTitleColor;
    self.titleLabel.font = self.config.monthTitleFont;
    
    // 更新箭头按钮样式
    [self.leftArrowButton setTitleColor:self.config.monthTitleColor forState:UIControlStateNormal];
    [self.rightArrowButton setTitleColor:self.config.monthTitleColor forState:UIControlStateNormal];
    
    // 更新日期格式
    self.dateFormatter.dateFormat = self.config.monthFormat;
    self.titleLabel.text = [self formatDateForDisplay:self.currentDate];
}

#pragma mark - Public Methods

- (void)updateCurrentDate:(NSDate *)date {
    if (!date) return;
    
    self.currentDate = date;
    self.titleLabel.text = [self formatDateForDisplay:date];
}

- (void)updateWithConfig:(KooCalendarConfig *)config {
    if (!config) return;
    
    self.config = config;
    [self updateAppearance];
}

- (void)setTitleText:(NSString *)title {
    if (!title) return;
    
    self.titleLabel.text = title;
}

- (void)setShowNavigationArrows:(BOOL)show animated:(BOOL)animated {
    self.showNavigationArrows = show;
    
    if (animated) {
        [UIView animateWithDuration:0.3 animations:^{
            self.leftArrowButton.alpha = show ? 1.0 : 0.0;
            self.rightArrowButton.alpha = show ? 1.0 : 0.0;
        } completion:^(BOOL finished) {
            self.leftArrowButton.hidden = !show;
            self.rightArrowButton.hidden = !show;
        }];
    } else {
        self.leftArrowButton.hidden = !show;
        self.rightArrowButton.hidden = !show;
        self.leftArrowButton.alpha = show ? 1.0 : 0.0;
        self.rightArrowButton.alpha = show ? 1.0 : 0.0;
    }
}

#pragma mark - Private Methods

- (NSString *)formatDateForDisplay:(NSDate *)date {
    if (!date || !self.dateFormatter) return @"";
    
    return [self.dateFormatter stringFromDate:date];
}

#pragma mark - Action Methods

- (void)titleTapped:(UITapGestureRecognizer *)gesture {
    if (self.titleClickBlock) {
        self.titleClickBlock();
    }
}

- (void)leftArrowTapped:(UIButton *)button {
    if (self.leftArrowClickBlock) {
        self.leftArrowClickBlock();
    }
}

- (void)rightArrowTapped:(UIButton *)button {
    if (self.rightArrowClickBlock) {
        self.rightArrowClickBlock();
    }
}

#pragma mark - Setters

- (void)setCurrentDate:(NSDate *)currentDate {
    _currentDate = currentDate;
    
    if (self.titleLabel) {
        self.titleLabel.text = [self formatDateForDisplay:currentDate];
    }
}

- (void)setConfig:(KooCalendarConfig *)config {
    _config = config;
    
    if (self.titleLabel) {
        [self updateAppearance];
    }
}

- (void)setShowNavigationArrows:(BOOL)showNavigationArrows {
    _showNavigationArrows = showNavigationArrows;
    
    if (self.leftArrowButton && self.rightArrowButton) {
        self.leftArrowButton.hidden = !showNavigationArrows;
        self.rightArrowButton.hidden = !showNavigationArrows;
    }
}

#pragma mark - Lazy Loading

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.userInteractionEnabled = YES;
        
        // 确保有合适的默认日期显示
        NSDate *displayDate = self.currentDate ?: [NSDate date];
        _titleLabel.text = [self formatDateForDisplay:displayDate];
        
        // 添加标题点击手势
        UITapGestureRecognizer *titleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleTapped:)];
        [_titleLabel addGestureRecognizer:titleTap];
        
        // 应用当前配置的样式
        if (self.config) {
            _titleLabel.textColor = self.config.monthTitleColor;
            _titleLabel.font = self.config.monthTitleFont;
        }
    }
    return _titleLabel;
}

- (UIButton *)leftArrowButton {
    if (!_leftArrowButton) {
        _leftArrowButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_leftArrowButton setTitle:@"<" forState:UIControlStateNormal];
        _leftArrowButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [_leftArrowButton addTarget:self action:@selector(leftArrowTapped:) forControlEvents:UIControlEventTouchUpInside];
        _leftArrowButton.hidden = !self.showNavigationArrows;
        
        // 应用当前配置的样式
        if (self.config) {
            [_leftArrowButton setTitleColor:self.config.monthTitleColor forState:UIControlStateNormal];
        }
    }
    return _leftArrowButton;
}

- (UIButton *)rightArrowButton {
    if (!_rightArrowButton) {
        _rightArrowButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_rightArrowButton setTitle:@">" forState:UIControlStateNormal];
        _rightArrowButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [_rightArrowButton addTarget:self action:@selector(rightArrowTapped:) forControlEvents:UIControlEventTouchUpInside];
        _rightArrowButton.hidden = !self.showNavigationArrows;
        
        // 应用当前配置的样式
        if (self.config) {
            [_rightArrowButton setTitleColor:self.config.monthTitleColor forState:UIControlStateNormal];
        }
    }
    return _rightArrowButton;
}

@end 